/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package client.dal;

import client.entity.Cv;
import client.entity.CvCategory;
import client.entity.CvLocation;
import client.entity.Education;
import client.entity.EmploymentHistory;
import java.util.ArrayList;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import util.HibernateUtil;

/**
 *
 * @author mrlucky
 */
public class CvDAL {

    public CvDAL() {
    }
    
    public void insertCV(Cv cv) {
        SessionFactory sf = HibernateUtil.getSessionFactory();
        Session ss = sf.openSession();
        ss.beginTransaction().begin();
        ss.save(cv);
        ss.beginTransaction().commit();
        ss.flush();
        ss.close();
    }
    
    public void updateCV(Cv cv) {
        SessionFactory sf = HibernateUtil.getSessionFactory();
        Session ss = sf.openSession();
        ss.beginTransaction().begin();
        ss.update(cv);
        ss.beginTransaction().commit();
        ss.flush();
        ss.close();
    }
    
    public ArrayList<Cv> loadCvByWorkerID(int workerID)
    {
        SessionFactory sf = HibernateUtil.getSessionFactory();
        Session ss = sf.openSession();
        ss.beginTransaction().begin();
        ArrayList<Cv> list = (ArrayList<Cv>) ss.createQuery("from Cv c where c.worker.id = " + workerID).list();
        ss.beginTransaction().commit();
        ss.close();
        return list;
    }
    
    public void insertCvCate(ArrayList<CvCategory> listcc) {
        SessionFactory sf = HibernateUtil.getSessionFactory();
        Session ss = sf.openSession();
        ss.beginTransaction().begin();
        for (CvCategory cc : listcc) {
            ss.save(cc);
        }
        ss.beginTransaction().commit();
        ss.flush();
        ss.close();
    }    
    
    public void insertCvLoca(ArrayList<CvLocation> listcl) {
        SessionFactory sf = HibernateUtil.getSessionFactory();
        Session ss = sf.openSession();
        ss.beginTransaction().begin();
        for (CvLocation cl : listcl) {
            ss.save(cl);
        }
        ss.beginTransaction().commit();
        ss.flush();
        ss.close();
    }
    
   
    public Cv loadCvById(String cvId)
    {
        SessionFactory sf = HibernateUtil.getSessionFactory();
        Session ss = sf.openSession();
        ss.beginTransaction().begin();
        Cv cv = (Cv) ss.createQuery("from Cv cv where cv.id ="+cvId).uniqueResult();
        ss.beginTransaction().commit();
        ss.close();
        return cv;
    }
    
    public ArrayList<Education> loadEduByCvId(int cvId)
    {
        SessionFactory sf = HibernateUtil.getSessionFactory();
        Session ss = sf.openSession();
        ss.beginTransaction().begin();
        ArrayList<Education> list = (ArrayList<Education>) ss.createQuery("from Education e where e.cv.id = "+cvId).list();
        ss.beginTransaction().commit();
        ss.flush();
        ss.close();
        return list;
    }
    
    public ArrayList<EmploymentHistory> loadEmpHistByCvId(int cvId)
    {
        SessionFactory sf = HibernateUtil.getSessionFactory();
        Session ss = sf.openSession();
        ss.beginTransaction().begin();
        ArrayList<EmploymentHistory> list = (ArrayList<EmploymentHistory>) ss.createQuery("from EmploymentHistory eh where eh.cv.id = "+cvId).list();
        ss.beginTransaction().commit();
        ss.flush();
        ss.close();
        return list;
    }
    
    public void deleteCvCateByRecId(int cvId)
    {
        SessionFactory sf = HibernateUtil.getSessionFactory();
        Session ss = sf.openSession();
        ss.beginTransaction().begin();
        ss.createQuery("delete from CvCategory cc where cc.cv.id ="+cvId).executeUpdate();
        ss.beginTransaction().commit();
        ss.flush();
        ss.close();
    }
    
    public void deleteCvLocByRecId(int cvId)
    {
        SessionFactory sf = HibernateUtil.getSessionFactory();
        Session ss = sf.openSession();
        ss.beginTransaction().begin();
        ss.createQuery("delete from CvLocation cl where cl.cv.id ="+cvId).executeUpdate();
        ss.beginTransaction().commit();
        ss.flush();
        ss.close();
    }
    
    public void deleteCv(Cv cv)
    {
        SessionFactory sf = HibernateUtil.getSessionFactory();
        Session ss = sf.openSession();
        ss.beginTransaction().begin();
        ss.delete(cv);
        ss.beginTransaction().commit();
        ss.flush();
        ss.close();
    }
    
    public void deleteEdu(int cvId)
    {
        SessionFactory sf = HibernateUtil.getSessionFactory();
        Session ss = sf.openSession();
        ss.beginTransaction().begin();
        ss.createQuery("delete from Education e where e.cv.id ="+cvId).executeUpdate();
        ss.beginTransaction().commit();
        ss.flush();
        ss.close();
    }
    
    public void deleteEmpHis(int cvId)
    {
        SessionFactory sf = HibernateUtil.getSessionFactory();
        Session ss = sf.openSession();
        ss.beginTransaction().begin();
        ss.createQuery("delete from EmploymentHistory eh where eh.cv.id ="+cvId).executeUpdate();
        ss.beginTransaction().commit();
        ss.flush();
        ss.close();
    }
    
    public void activeCv(Cv cv)
    {
        SessionFactory sf = HibernateUtil.getSessionFactory();
        Session ss = sf.openSession();
        ss.beginTransaction().begin();
        ss.update(cv);
        ss.beginTransaction().commit();
        ss.flush();
        ss.close();
    }
    
    public void deactiveCv()
    {
        SessionFactory sf = HibernateUtil.getSessionFactory();
        Session ss = sf.openSession();
        ss.beginTransaction().begin();
        ss.createQuery("update Cv c set c.active = false").executeUpdate();
        ss.beginTransaction().commit();
        ss.flush();
        ss.close();
    }
}
